<?php
//require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['playerID'])) {

  include_once './db_bb_connect.php';

  $DNP = $_POST['DNP'];
  $selectedgame = $_POST['selectedgame'];
  $season = $_POST['season'];
  $playerID = $_POST['playerID'];

  if ($DNP != 'no'){ 

    $sql = "INSERT INTO stats (playerID,season,gamenumber,DNP)
    VALUES ('$playerID','$season', $selectedgame ,'$DNP')";

    if ($db->query($sql)) {  
      $sql2 = "SELECT firstname
      FROM users
      WHERE user_key = '$playerID'";

      $result2=$db->query($sql2);
      $row2=$result2->fetch_assoc();
      $firstname = $row2['firstname'];
      echo "<br><br><center>";
      echo "Stats of $firstname are added to game $selectedgame of season $season";
      echo "<br><br></center>";

    } else {
        echo "Error: " . $sql . "<br>" . $db->error;
    }
   
  } else {

    foreach ($_POST as $key => $value) {
      if ($value == '') {
        $value = ":null";
        $$key = $value;
      } else {
        	$$key = $value;
      }
    }
  
    if ($season == '2017/2018') {

      $sql =  "INSERT INTO stats (playerID, season, gamenumber, ORB, DRB, TOV, ST, FTscored, FTtotal, FGscored, FGtotal, DNP, PF, FG_outside_scored, FG_outside_total, THPscored, THPtotal)
      VALUES ('$playerID', '$season', $selectedgame, $ORB, $DRB, $TOV, $ST, $FTscored, $FTtotal, $FGscored, $FGtotal, '$DNP', $PF, $FGoutsidescored , $FGoutsidetotal, $THPscored, $THPtotal)";
    
    } else if ($season >= '2018/2019') {

      $MPsplit = explode(":", $MP);
      $MP = 60 * (int)$MPsplit[0] + (int)$MPsplit[1];

      $sql =    "INSERT INTO stats (playerID, season, gamenumber, ORB, DRB, TOV, ST, FTscored, FTtotal, FGscored, FGtotal, DNP, PF, FG_outside_scored, FG_outside_total, THPscored, THPtotal, MP, S5, AST, BS)
      VALUES ('$playerID', '$season', $selectedgame, $ORB, $DRB, $TOV, $ST, $FTscored, $FTtotal, $FGscored, $FGtotal, '$DNP', $PF, $FGoutsidescored , $FGoutsidetotal, $THPscored, $THPtotal, $MP, '$S5', $AST, $BS)";

    } else {

    $sql = "INSERT INTO stats (playerID, season, gamenumber, ORB, DRB, TOV, ST, FTscored, FTtotal, FGscored, FGtotal, DNP, PF)
    VALUES ('$playerID', '$season', $selectedgame, $ORB, $DRB, $TOV, $ST, $FTscored, $FTtotal, $FGscored, $FGtotal, '$DNP', $PF)";
  
    }


  try{
    $dbpdo = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
    $dbpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbpdo->prepare($sql);
	  $stmt->bindValue(":null", 0, PDO::PARAM_INT);
	  if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
	  } 

	  $sql2 = "SELECT firstname
		FROM users
 		WHERE user_key = '$playerID'";

		$result2=$db->query($sql2);
		$row2=$result2->fetch_assoc();
		$firstname = $row2['firstname'];
		echo "<br><br><center>";
		echo "Stats of $firstname are added to game $selectedgame of season $season";
		echo "<br><br></center>";
		
  } catch (PDOException $e) {    
    $error = $e->getMessage();
    echo $error;
  }

  } 

}

 ?>

